var SpriteLoader = require('RaiSpriteLoader');
var self;

cc.Class({
    extends: cc.Component,

    properties: {
        back: cc.Node,
        btns: [cc.Button],
        selectedPos: [cc.Node],
        originPos: [cc.v2],
        selectTerminal: [String],
    },

    // use this for initialization
    onLoad: function () {
        self = this;
        this.gameMain = cc.find('Canvas').getComponent('HelloWorld');
        this.terminalCheckPanel = cc.find('TerminalCheckPanel', this.node).getComponent('TerminalCheckPanel');
        // 事件拦截
        this.back.on(cc.Node.EventType.TOUCH_START, function(){ return true; }, this);
        //填充终端卡数组
        this.selectTerminal = [null, null, null, null, null];
        //按钮添加事件
        for(let i = 0;i < this.btns.length;i++)
        {
            let btn = this.btns[i];
            btn.node.on(cc.Node.EventType.TOUCH_END, ()=>{
                //当终端卡位于已选择区时让终端卡退回待选区
                for(let i in this.selectTerminal)
                {
                    let selectName = this.selectTerminal[i];
                    if(btn.name == selectName)
                    {
                        this.onUnSelectTerminal(btn.name, btn.node);
                        return;
                    }
                }
                //显示终端卡介绍
                this.showTerminalTip(btn.name, btn.node);
            }, this);
        }
    },

    //显示终端卡介绍
    showTerminalTip: function(btnName, btnNode){
        this.terminalCheckPanel.show(btnName, btnNode, this.onSelectTerminal);
    },

    init: function(group, callback){
        //记录回调
        this._callback = callback;
        //根据颜色改变图标
        for(let i = 0;i < this.btns.length;i++)
        {
            let btn = this.btns[i];
            btn.name = btn.name.replace('<Button>', '');
            SpriteLoader.load(group + btn.name, sprite => {
                btn.getComponent(cc.Sprite).spriteFrame = sprite;
            });
        }
        //按钮分别朝不同方向移动
        let r = 200;
        for(let i = 0;i < this.btns.length;i++)
        {
            let btn = this.btns[i];
            let angle = 360 * i / this.btns.length;
            let newX = btn.node.getPosition().x + Math.sin(angle / 180 * Math.PI) * r;
            let newY = btn.node.getPosition().y + Math.cos(angle / 180 * Math.PI) * r;
            let v2 = cc.v2(newX, newY);
            this.originPos[btn.name] = v2;
            //每个按钮开始移动时间错开
            this.scheduleOnce(function(){
                btn.node.runAction(cc.moveBy(0.3, v2));
            }, i * 0.04);
        };

        let node = this.node;
        if(node.active)
            return false;
        node.active = true;
        let actShow = cc.fadeIn(0.15);
        node.runAction(actShow);
        return true;
    },

    // 隐藏对象
    // @obj: 将隐藏的对象
    hide: function(){
        var obj = self.node;
        if(!obj.active)
            return false;
        var act1=cc.fadeOut(0.15);
        var callback = cc.callFunc(() => {
            obj.active = false;
        });
        var seq = cc.sequence(act1, callback);
        obj.runAction(seq);
        return true;
    },

    //选择终端卡
    onSelectTerminal: function(selectName, btnNode, time = 0.2){
        let terminals = self.selectTerminal;
        //记录选择的终端卡
        let index = -1;
        let blankCount = 0;
        for(let i = 0; i < terminals.length; i++)
        {
            if(terminals[i] == null || terminals[i] == undefined)
            {
                if(index < 0)
                {
                    index = i;
                    terminals[i] = selectName;
                }
                else{
                    blankCount++;
                }
            }
        }
        //移动终端卡位置
        let pos = self.selectedPos[index].getPosition();
        btnNode.active = true;
        btnNode.runAction(cc.spawn(cc.moveTo(time, pos), cc.scaleTo(time, 1)), cc.callFunc(() => {
            btnNode.position = pos;
        }));
        //选择的终端卡没有空位时
        if(blankCount == 0)
        {
            //终端卡选择完毕
            self.onSelectFinish();
        }
    },

    //取消选择
    onUnSelectTerminal: function(selectName, btnNode){
        let terminals = self.selectTerminal;
        //取消记录终端卡
        let index = terminals.indexOf(selectName);
        delete terminals[index];
        //把终端卡移回原位
        let pos = self.originPos[selectName];
        btnNode.runAction(cc.moveTo(0.2, pos));
    },

    //终端卡选择完成
    onSelectFinish: function()
    {
        let terminals = self.selectTerminal;
        let result = new Array();
        for(let i = 0; i < terminals.length; i++)
        {
            let selectName = terminals[i];
            if(selectName == undefined)
                continue;
            switch(selectName)
            {
                case 'lineboost':
                    result.push('lb');
                break;
                case 'firewall':
                    result.push('fw');
                break;
                case 'viruscheck':
                    result.push('vc');
                break;
                case 'notfound':
                    result.push('nf');
                break;
                case 'randomverification':
                    result.push('rv');
                break;
                case 'zerodayattack':
                    result.push('zda');
                break;
                case 'wormdiffusion':
                    result.push('wd');
                break;
                case 'sandbox':
                    result.push('sb');
                break;
            }
        }
        self.hide();
        self.gameMain.selectTerminal = result;
        if(self._callback != null)
            self._callback();
    },
});
